<template>
	<!-- 原料不良池页面 -->
	<a-card :bordered="false">
		<!-- 顶部筛选 -->
		<div class="table-page-search-wrapper">
			<a-form layout="inline" labelAlign="right">
				<a-row :gutter="48">
					<a-col :md="8" :sm="24">
						<a-form-item label="不良池编号">
							<a-input v-model="queryParam.number" placeholder="请输入" />
						</a-form-item>
					</a-col>
					<a-col :md="8" :sm="24">
						<a-form-item label="物料编号">
							<a-input v-model="queryParam.materialCode" placeholder="请输入" />
						</a-form-item>
					</a-col>
					<a-col :md="24" :sm="24">
						<a-form-item label="状 态">
							<!-- {{ $businessEnum.MATERIAL_TYPE_ENUM.packing }} -->
							<a-radio-group default-value="" button-style="solid" v-model="queryParam.defectiveStatus">
								<a-radio-button value="">全部</a-radio-button>
								<a-radio-button value="0">待审核</a-radio-button>
								<a-radio-button value="1">待处理</a-radio-button>
								<!-- <a-radio-button value="2"
									v-if="![$businessEnum.MATERIAL_TYPE_ENUM.packing.id, $businessEnum.MATERIAL_TYPE_ENUM.cable.id].includes(materialType)">待补发</a-radio-button> -->
								<a-radio-button value="3">已扣款</a-radio-button>
								<a-radio-button value="4"
									v-if="![$businessEnum.MATERIAL_TYPE_ENUM.packing.id, $businessEnum.MATERIAL_TYPE_ENUM.cable.id].includes(materialType)">待入库</a-radio-button>
								<a-radio-button value="5"
									v-if="![$businessEnum.MATERIAL_TYPE_ENUM.packing.id, $businessEnum.MATERIAL_TYPE_ENUM.cable.id].includes(materialType)">已入库</a-radio-button>
								<a-radio-button value="6">驳回待提交</a-radio-button>
								<a-radio-button value="30">待扣款</a-radio-button>
								<a-radio-button value="51">已入库（部分扣款）</a-radio-button>
							</a-radio-group>
						</a-form-item>
					</a-col>
					<a-col :md="24" :sm="24">
						<a-form-item label="供  应  商">
							<FCOrgRadioGroup1 v-model="queryParam.orgIdSupplier" :filter-types="materialType" />
						</a-form-item>
					</a-col>
					<a-col :md="8" :sm="24">
						<span class="table-page-search-submitButtons">
							<a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
							<a-button type=""
								@click="() => { queryParam = { orgIdSupplier: '', defectiveStatus: '' }; $refs.table.refresh(true) }">重置</a-button>
							<a-button type="danger" icon="plus" @click="$refs.addFormNew.init('add')">新增不良池</a-button>
							<!-- 原料不良池导出 -->
							<span v-if="$businessEnum.MATERIAL_TYPE_ENUM.material.id == materialType">
								<Export text="导出" v-if="hasBtnPremission(['badpool:export:material_price'])" title="导出"
									url="/emsDefectivePoolRecord/export" :isBody="true" :obj="{
										...queryParam
									}" />
								<Export text="导出" v-if="hasBtnPremission(['badpool:export:material_noprice'])" title="导出"
									url="/emsDefectivePoolRecord/notPriceExport" :isBody="true" :obj="{
										...queryParam
									}" />
							</span>
							<!-- 定制料不良池导出 -->
							<span v-if="$businessEnum.MATERIAL_TYPE_ENUM.customized.id == materialType">
								<Export text="导出" v-if="hasBtnPremission(['badpool:export:customized_price'])" title="导出"
									url="/emsDefectivePoolRecord/export" :isBody="true" :obj="{
										...queryParam
									}" />
								<Export text="导出" v-if="hasBtnPremission(['badpool:export:customized_noprice'])" title="导出"
									url="/emsDefectivePoolRecord/notPriceExport" :isBody="true" :obj="{
										...queryParam
									}" />
							</span>
							<!-- 包材不良池导出 -->
							<span v-if="$businessEnum.MATERIAL_TYPE_ENUM.packing.id == materialType">
								<Export text="导出" v-if="hasBtnPremission(['badpool:export:packing_price'])" title="导出"
									url="/emsDefectivePoolRecord/export" :isBody="true" :obj="{
										...queryParam
									}" />
								<Export text="导出" v-if="hasBtnPremission(['badpool:export:packing_noprice'])" title="导出"
									url="/emsDefectivePoolRecord/notPriceExport" :isBody="true" :obj="{
										...queryParam
									}" />
							</span>
							<!-- 线缆不良池导出 -->
							<span v-if="$businessEnum.MATERIAL_TYPE_ENUM.cable.id == materialType">
								<Export text="导出" v-if="hasBtnPremission(['badpool:export:cable_price'])" title="导出"
									url="/emsDefectivePoolRecord/export" :isBody="true" :obj="{
										...queryParam
									}" />
								<Export text="导出" v-if="hasBtnPremission(['badpool:export:cable_noprice'])" title="导出"
									url="/emsDefectivePoolRecord/notPriceExport" :isBody="true" :obj="{
										...queryParam
									}" />
							</span>
						</span>
					</a-col>
				</a-row>
			</a-form>
		</div>
		<s-table ref="table" style="margin-top: 15px;" size="default" :columns="columns1" :data="loadData" :alert="true">
			<!-- 详情 -->
			<span slot="defectiveStatus" slot-scope="text, record">
				<a-tag color="gray" v-if="text == 0">待审核</a-tag>
				<a-tag color="blue" v-if="text == 1">待处理</a-tag>
				<a-tag color="orange" v-if="text == 2">待补发</a-tag>
				<a-tag color="cyan" v-if="text == 3">已扣款</a-tag>
				<a-tag color="pink" v-if="text == 4">待入库</a-tag>
				<a-tag color="green" v-if="text == 5">已入库</a-tag>
				<a-tag color="red" v-if="text == 6">驳回待提交</a-tag>
				<a-tag color="#f50" v-if="text == 30">待扣款</a-tag>
				<a-tag color="#f50" v-if="text == 51">已入库（部分扣款）</a-tag>
			</span>
			<!-- 操作 -->
			<span slot="action" slot-scope="text, record">
				<a @click="$refs.detailFormNew.init('detail', record)">详情</a>
				<a-divider type="vertical" v-if="managerOrFinance() && record.defectiveStatus == 0" />
				<a @click="$refs.detailFormNew.examine('examine', record)"
					v-if="managerOrFinance() && record.defectiveStatus == 0">审核</a>
				<a-divider type="vertical" v-if="managerOrFinance() && record.defectiveStatus == 30" />
				<a @click="$refs.detailFormNew.deduction('deduction', record)"
					v-if="managerOrFinance() && record.defectiveStatus == 30">扣款</a>
				<a-divider type="vertical" v-if="record.defectiveStatus == 6" />
				<a @click="$refs.detailFormNew.reSubmit('reSubmit', record)" v-if="record.defectiveStatus == 6">重新提交</a>
				<!-- <a-divider type="vertical" />
				<a @click="$refs.reissueOrStoreRecord.init('reissue', record)">补发记录</a> -->
				<a-divider type="vertical" />
				<a @click="$refs.reissueOrStoreRecord.init('store', record)">入库记录</a>
			</span>
		</s-table>
		<!--  新增编辑详情弹框  -->
		<addForm :materialType="materialType" ref="addForm" @ok="$refs.table.refresh()" />
		<detailFormNew :materialType="materialType" ref="detailFormNew" @ok="$refs.table.refresh()" />
		<addFormNew ref="addFormNew" @ok="$refs.table.refresh()" :materialType="materialType" />
		<reissueOrStoreRecord ref="reissueOrStoreRecord" @ok="$refs.table.refresh()" />
	</a-card>
</template>

<script>
import { STable } from '@/components'
import { pageNew, add } from '@/api/modular/main/badPool'
import addForm from './addForm'
import addFormNew from './addFormNew.vue'
import reissueOrStoreRecord from './reissueOrStoreRecord.vue'
// import detailForm from './detailForm'
import detailFormNew from './detailFormNew.vue'
import { getUrlParam, MATERIAL_TYPE_ENUM, ORDER_TYPE_ENUM } from '@/utils/util'
import FCOrgRadioGroup1 from '@/components/FcOrgRadioGroup/FCOrgRadioGroup1'


export default {
	components: {
		STable,
		addForm,
		detailFormNew,
		FCOrgRadioGroup1,
		addFormNew,
		reissueOrStoreRecord
	},
	data() {
		return {
			queryParam: {},  // 查询参数
			selectedRows: [],
			columns: [
				{
					title: '序号',
					width: '80px',
					customRender: (text, record, index) => `${index + 1}`
				},
				{
					title: '采购单号',
					dataIndex: 'orderNumber'
				},
				{
					title: '物料名称',
					dataIndex: 'materialName'
				},
				{
					title: '料号',
					dataIndex: 'materialCode'
				},
				{
					title: '规格',
					dataIndex: 'materialModelNumber'
				},
				{
					title: '数量',
					dataIndex: 'num'
				},
				{
					title: '备注',
					dataIndex: 'remark',
					customRender: (text, record, index) => text ? text : '-'
				},
				{
					title: '状态',
					dataIndex: 'defectiveStatus',
					scopedSlots: { customRender: 'defectiveStatus' }
				},
				{
					title: '供应商',
					dataIndex: 'orgName'
				},
				{
					title: '创建时间',
					dataIndex: 'createTime'
				},
				{
					title: '操作',
					align: 'center',
					width: '250px',
					scopedSlots: { customRender: 'action' }
				}
			],
			columns1: [
				{
					title: '序号',
					width: '80px',
					customRender: (text, record, index) => `${index + 1}`
				},
				{
					title: '不良池编号',
					dataIndex: 'number'
				},
				{
					title: '供应商',
					dataIndex: 'orgIdSupplierName'
				},
				{
					title: '状态',
					dataIndex: 'defectiveStatus',
					scopedSlots: { customRender: 'defectiveStatus' }
				},
				{
					title: '创建时间',
					dataIndex: 'createTime'
				},
				{
					title: '备注',
					dataIndex: 'remark',
					customRender: (text, record, index) => text ? text : '-'
				},
				{
					title: '操作',
					align: 'center',
					width: '250px',
					scopedSlots: { customRender: 'action' }
				}
			],
			// 加载数据方法 必须为 Promise 对象
			loadData: parameter => {
				let query = {
					...this.queryParam,
					...parameter,
					materialType: this.materialType
				}
				delete query.timeList
				return pageNew(query).then((res) => {
					return res.data
				})
			},
			scheduleProductionVisible: false,
			selectedRowKeys: [],
			selectedRows: [],
			defaultExpandedKeys: [],
			replaceFields: {
				key: 'id',
			},
		}
	},

	computed: {
		urlParam() {
			return getUrlParam(this.$route.path)
		},
		poolType() {
			return this.urlParam.poolType;
		},
		materialType() {
			return this.urlParam.materialType;
		},
		ORDER_TYPE_ENUM() {
			return ORDER_TYPE_ENUM;
		},
		materialTypeList() {
			let value = [{ id: '', name: '全部', code: 'all' }]
			for (const key in MATERIAL_TYPE_ENUM) {
				value.push(MATERIAL_TYPE_ENUM[key])
			}
			return value
		},
	},
	watch: {
		'$route.path': function () {
			this.queryParam.materialType = this.urlParam.materialType
			setTimeout(() => {
				this.$refs.table.refresh(true)
				this.selectedRows.length = []
			}, 100)
		}
	},
	methods: {
		handleOk() {
			this.$refs.table.refresh()
		},
		// 表格选择按钮点击事件
		handleClickSelectBtn(record) {
			let i = this.selectedRows.findIndex(item => item.orderId === record.orderId)
			if (i == -1) {
				this.selectedRows.push(record)
			} else {
				this.selectedRows.splice(i, 1)
			}
		},
		// 清除选中
		clearSelect() {
			this.selectedRows = []
		},
		// 核算完成点击事件
		checkSuccess() {
			if (this.selectedRows.length == 0) {
				return this.$message.warning('请选择要核算的订单')
			}
			let query = {
				orderIds: this.selectedRows.map(item => item.orderId)
			}
			checkApi(query).then(res => {
				if (res.code == 200) {
					this.$message.success('核算成功')
					this.handleOk()
					this.clearSelect()
				}
			})
		},
		onSelectChange(selectedRowKeys, selectedRows) {
			this.selectedRowKeys = selectedRowKeys
			this.selectedRows = selectedRows
		},
	}
}
</script>

<style lang="less">
.table-operator {
	margin-bottom: 18px;
}

button {
	margin-right: 8px;
}
</style>
